package com.qjq.glasses_shopping.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qjq.glasses_shopping.mapper.BrandMapper;
import com.qjq.glasses_shopping.pojo.Brand;
import com.qjq.glasses_shopping.result.BusException;
import com.qjq.glasses_shopping.result.CodeEnum;
import com.qjq.glasses_shopping.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;

    /**
     * 根据id查询品牌管理
     * @param id
     * @return
     */
    @Override
    public Brand findById(Long id) {

        if (id == null || id <= 0){
            throw new BusException(CodeEnum.PARAMETER_ERROR); // 模拟业务异常
        }
        return brandMapper.selectById(id);
    }

    @Override
    public List<Brand> findAll() {
        return brandMapper.selectList(null);
    }

    // todo:新增异常
    @Override
    public void add(Brand brand) {
        brandMapper.insert(brand);
    }

    @Override
    public void update(Brand brand) {
        brandMapper.updateById(brand);
    }

    @Override
    public void delete(Long id) {
        brandMapper.deleteById(id);
    }

    @Override
    public Page<Brand> search(Brand brand, int page, int size) {
        QueryWrapper<Brand> queryWrapper = new QueryWrapper<Brand>();
        //如果品牌名字不为空
        if (brand!=null && brand.getName()!=null && brand.getName().length()>0){
            queryWrapper.like("name",brand.getName());
        }
        Page<Brand> brandPage = brandMapper.selectPage(new Page<>(page, size), queryWrapper);
        return brandPage;
    }
}
